package com.admin.controller.declare;

import com.admin.common.shiro.ShiroUtils;
import com.admin.common.util.DateUtils;
import com.admin.common.util.YsCollectionUtil;
import com.admin.controller.base.BaseController;
import com.admin.req.declare.FlagReq;
import com.admin.req.declarereturn.ReturnReq;
import com.admin.req.score.ScoreReq;
import com.admin.service.DeclareProvinceService;
import com.admin.service.SysParamService;
import com.admin.vo.PageVO;
import com.admin.vo.ResultVO;
import com.admin.vo.city.CitySampleReq;
import com.admin.vo.declare.DeclareBatchScoreInfoVO;
import com.admin.vo.declare.DeclareListVO;
import com.admin.vo.declare.DeclareSampleVO;
import com.admin.vo.declare.province.ProvinceFlagVO;
import com.admin.vo.declareDetail.DeclareDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 省级评分管理
 * 
 * @author yangzhongliang
 * @date 2022/05/16
 */
@Api(description = "省级评分管理")
@RestController
@RequestMapping(value = "/declareProvince")
public class DeclareProvinceController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(DeclareProvinceController.class);

    @Autowired
    private DeclareProvinceService declareProvinceService;
    @Autowired
    private SysParamService sysParamService;


    @ApiOperation(value = "省级评分管理-本年申报")
    @RequiresPermissions("provinceScore:list")
    @GetMapping(value = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "regionIds", value = "乡镇街道", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "scoreStatus", value = "评分状态", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "declareType", value = "申报类型 0 正常 1 提标", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "declareStatus", value = "申报状态", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isCitySample", value = "是否是市样本 0 否 1 是", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isProvinceSample", value = "是否是省级样本 0 否 1 是", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "minScore", value = "省评分区间最小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "maxScore", value = "省评分区间最大", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "villageName", value = "乡村社区名称", dataType = "string", paramType = "query"),
    })
    public ResultVO<PageVO<DeclareListVO>> list(Integer pageIndex, Integer pageSize, Integer startTime, Integer endTime,String regionIds,
                                                Integer scoreStatus, Integer declareType , Integer declareStatus,
                                                Integer isCitySample, Integer isProvinceSample,Integer minScore,Integer maxScore, String villageName) {
        //本轮申报开始时间
        Integer  declareStartTime = sysParamService.getCurrentDeclareStartTime();
        if (startTime == null || startTime == 0 || startTime < declareStartTime) {
            startTime = declareStartTime;
        }
        Map<String, Object> params = buildParams(startTime, endTime, regionIds, scoreStatus, declareType, declareStatus, isCitySample, isProvinceSample, minScore, maxScore, villageName);
        return declareProvinceService.getPageList(pageIndex, pageSize, params);
    }


    @ApiOperation(value = "省级评分管理-历史申报")
    @GetMapping(value = "/historyList")
    @RequiresPermissions("provinceScore:list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "regionIds", value = "乡镇街道", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "scoreStatus", value = "评分状态", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "declareType", value = "申报类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "declareStatus", value = "申报状态", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isCitySample", value = "是否是市样本 0 否 1 是", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isProvinceSample", value = "是否是省级样本 0 否 1 是", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "minScore", value = "省评分区间最小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "maxScore", value = "省评分区间最大", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "villageName", value = "乡村社区名称", dataType = "string", paramType = "query"),
    })
    public ResultVO<PageVO<DeclareListVO>> historyList(Integer pageIndex, Integer pageSize, Integer startTime, Integer endTime,String regionIds,
                                                       Integer scoreStatus, Integer declareType , Integer declareStatus,
                                                       Integer isCitySample, Integer isProvinceSample, Integer minScore, Integer maxScore, String villageName) {
        //历史申报，申报结束时间 小于当前申报开始时间的
        Integer  declareStartTime = sysParamService.getCurrentDeclareStartTime() - 1;
        if (null == endTime || endTime == 0 || endTime > declareStartTime) {
            endTime = declareStartTime;
        }
        Map<String, Object> params = buildParams(startTime, endTime, regionIds, scoreStatus, declareType, declareStatus, isCitySample, isProvinceSample, minScore, maxScore, villageName);
        return declareProvinceService.getPageList(pageIndex, pageSize, params);
    }


    private Map<String,Object> buildParams(Integer startTime, Integer endTime,String regionIds, Integer scoreStatus, Integer declareType ,
                                           Integer declareStatus, Integer isCitySample, Integer isProvinceSample, Integer minScore,Integer maxScore, String villageName) {
        Map<String, Object> params = YsCollectionUtil.createHashMap();
        params.put("startTime", DateUtils.dateFormatStr(startTime));
        params.put("endTime", DateUtils.dateFormatStr(endTime));
        //根据乡镇查询
        if (StringUtils.isNotEmpty(regionIds)) {
            List<Integer> searchRegionIds = new ArrayList<>();
            String[] regionIdArray  = regionIds.split(",");
            searchRegionIds.add(Integer.valueOf(regionIdArray[regionIdArray.length - 1]));
            params.put("searchRegionIds", searchRegionIds);
        }
        params.put("scoreStatus", scoreStatus);
        params.put("declareType", declareType);
        params.put("declareStatus", declareStatus);
        params.put("isCitySample", isCitySample);
        params.put("isProvinceSample", isProvinceSample);
        params.put("provinceMinScore", minScore);
        params.put("provinceMaxScore", maxScore);
        params.put("villageName", villageName);
        //需要根据 省样本和未打分排序
        params.put("orderByProvinceSample", "true");

        return params;
    }


    @ApiOperation(value = "省级评分管理-列表 - 单个标记")
    @PostMapping(value = "/submitFlag")
    @RequiresPermissions("provinceScore:batchFlag")
    public ResultVO<Boolean> submitFlag(@RequestBody FlagReq flagReq) {
        return ResultVO.ok(declareProvinceService.flagDeclare(ShiroUtils.getUserId(), flagReq));
    }


    @ApiOperation(value = "省级评分管理-列表 - 详情(预览)")
    @GetMapping(value = "/detail")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "int", paramType = "query")
    public ResultVO<DeclareDetailVO> detail(Integer id) {

        return ResultVO.ok(declareProvinceService.detail(id));
    }


    @ApiOperation(value = "市级评分管理-列表 - 保存评分")
    @PostMapping(value = "/saveScore")
    @RequiresPermissions("provinceScore:score")
    public ResultVO save(@RequestBody ScoreReq scoreReq) {
        return declareProvinceService.saveScore(ShiroUtils.getUserId(), scoreReq);
    }


    @ApiOperation(value = "省级评分管理-列表 - 回退")
    @PostMapping(value = "/cityReturn")
    @RequiresPermissions("provinceScore:return")
    public ResultVO save(@RequestBody ReturnReq returnReq) {
        return declareProvinceService.provinceReturn(ShiroUtils.getUserId(), returnReq);
    }


    @ApiOperation(value = "省级评分管理-列表 - 去抽样 - 弹框信息")
    @GetMapping(value = "/toSample")
    @RequiresPermissions("provinceScore:smaple")
    @ApiImplicitParam(name = "declareType", value = "申报类型 0 正常 1 提标升级", required = true, dataType = "int", paramType = "query")
    public ResultVO<DeclareSampleVO> toSample(Integer declareType) {
        return ResultVO.ok(declareProvinceService.toSample(ShiroUtils.getUserId(), declareType));
    }


    @ApiOperation(value = "省级评分管理-列表 - 去抽样 - 开始抽样")
    @PostMapping(value = "/startSample")
    @RequiresPermissions("provinceScore:smaple")
    public ResultVO<DeclareSampleVO> startSample(@RequestBody CitySampleReq req) {
        return ResultVO.ok(declareProvinceService.saveSample(ShiroUtils.getUserId(), req.getDeclareType()
                , req.getSampleRate().intValue(), req.getSampleScope()));
    }


    @ApiOperation(value = "省级评分管理-列表 - 去批量赋分 - 弹框信息")
    @GetMapping(value = "/toBatchScore")
    @RequiresPermissions("provinceScore:batchScore")
    public ResultVO<DeclareBatchScoreInfoVO> toBatchScore() {
        return ResultVO.ok(declareProvinceService.getBatchScoreInfo(ShiroUtils.getUserId()));
    }


    @ApiOperation(value = "省级评分管理-列表 - 去批量赋分 - 确认赋分")
    @PostMapping(value = "/saveBatchScore")
    @RequiresPermissions("provinceScore:batchScore")
    public ResultVO<Integer> saveBatchScore() {
        return ResultVO.ok(declareProvinceService.batchScore(ShiroUtils.getUserId(), null));
    }


    @ApiOperation(value = "省级评分管理-列表 - 去批量标记 - 弹框信息")
    @PostMapping(value = "/toBatchFlag")
    @RequiresPermissions("provinceScore:batchFlag")
    public ResultVO<ProvinceFlagVO> toBatchFlag(@RequestBody FlagReq req) {
        return ResultVO.ok(declareProvinceService.toBatchFlag(req));
    }


    @ApiOperation(value = "省级评分管理-列表 - 保存批量标记 - 弹框信息")
    @PostMapping(value = "/saveBatchFlag")
    @RequiresPermissions("provinceScore:batchFlag")
    public ResultVO saveBatchFlag(@RequestBody FlagReq req) {
        return declareProvinceService.saveBatchFlag(req);
    }


}
